#!/bin/sh

#scp -B -q -i ../.ssh/private_rsa_2048 remote/mailq.sh nagios@192.168.2.100:/tmp/mailq.sh
#./check_by_ssh -l nagios -i ../.ssh/private_rsa_2048 -H 192.168.2.100 -C "cd /tmp && /tmp/mailq.sh"

#export > /tmp/varrssh.log
#export NAGIOS__HOSTSSH_IDENTITY='/usr/local/nagios/.ssh/private_rsa_2048'
#export NAGIOS__HOSTSSH_UPLOADDIR='/tmp'
#export NAGIOS__HOSTSSH_USERNAME='nagios'
#export NAGIOS__SERVICESSH_SCRIPT='/dev/null'

REPOSITORY=/usr/local/nagios/libexec/remote

SCRIPT=$1
shift

# master channel ouvert ?
msg=`ssh -o ControlPath=$NAGIOS__HOSTSSH_SOCKET -O check $NAGIOS_HOSTADDRESS 2>&1`
ret=$?
msg=`echo $msg|tr '\n' ' '`

if [ $ret -eq 255 ]
then
    echo "UNKNOWN - Could not access SSH master socket : $msg"
    exit 3
fi

scp -qB -o ControlPath=$NAGIOS__HOSTSSH_SOCKET $REPOSITORY/$SCRIPT $NAGIOS_HOSTADDRESS:$NAGIOS__HOSTSSH_UPLOADDIR/$SCRIPT && \
/usr/local/nagios/libexec/check_by_ssh -o ControlPath=$NAGIOS__HOSTSSH_SOCKET -H $NAGIOS_HOSTADDRESS -C "cd $NAGIOS__HOSTSSH_UPLOADDIR && $NAGIOS__HOSTSSH_UPLOADDIR/$SCRIPT $*"
